<template>
  <div class="card">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>社团添加详情</span>
        </div>
      </template>
      <div>
        <el-form :model="clubInfo" label-width="auto" style="max-width: 900px">
          <el-form-item label="社团名称">
            <el-input class="nameInput" v-model="clubInfo.name" />
          </el-form-item>
          <el-form-item label="社团负责人">
            <el-input v-if="showReact" v-model="adminName" style="width: 240px" placeholder="输入姓名关键词" />
            <el-button v-if="showReact" @click="lookupAdmin">查询负责人</el-button>
            <el-input class="adminInput" v-if="!showReact"disabled placeholder="当前用户" />
          </el-form-item>
          <el-form-item label="社团级别">
            <el-radio-group v-model="clubInfo.level">
              <el-radio :value=1>院级社团</el-radio>
              <el-radio :value=0>校级社团</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="社团简介">
            <el-input maxlength="200" show-word-limit class="intro" v-model="clubInfo.introduction" type="textarea" />
          </el-form-item>
          <div v-if="!showReact">
            <el-form-item v-if="!showReact" label="申请原因">
            <el-input v-if="!showReact" maxlength="200" show-word-limit class="reason" v-model="clubInfo.reason" type="textarea" />
          </el-form-item>
          </div>
          
        </el-form>
      </div>
      <template #footer>
        <el-button v-if="!showReact" id="submit" type="primary" @click="onSubmit">
          提交创建申请
        </el-button>
        <el-button v-if="showReact" id="create" type="primary" @click="onCreate">
          创建社团
        </el-button>

      </template>
    </el-card>
  </div>
  <el-drawer v-model="drawer" title="查询结果如下" direction='ltr' >
    <span>
      <el-select @change="newAdmin"
      v-model="adminSelect"
      placeholder="Select"
      size="large"
      style="width: 300px"
    >
      <el-option
       v-for="item in options" 
       :key="item.id"
       :label="item.id+`&nbsp&nbsp`+item.userName"
       :value="item.id"
      />
    </el-select>
    </span>
  </el-drawer>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import userStore from '@/stores/store';
import api from '@/api';
import showMessageTip from '@/utils/showMessage';

const showReact = ref(false);
const drawer=ref(false);
const store = userStore();
let adminName= ref();
const options=ref([]);
const adminSelect= ref();
const clubInfo = reactive({
  name: '',
  admin: '',
  level: -1,
  introduction: '',
  reason:''
})


const onSubmit = async () => {
  if(clubInfo.name===''||clubInfo.level===-1){
    showMessageTip('关键信息不能为空','warning');
    return;
  }
  else if(clubInfo.reason==''){
    showMessageTip('请填写申请原因','warning');
    return;
  }
  const applicationInfo= reactive({
    bigAffairType:1,
    affairType:1,
    value1:clubInfo.name,
    value4:clubInfo.introduction,
    value3:clubInfo.level,
    reason:clubInfo.reason
}) 
  const result =await api.applicateClub(applicationInfo);
  if(result[0]==0){
  const data =result[1].data;
  if(data.code===1){
    showMessageTip('创建成功','success');
    clubInfo.name='',
    clubInfo.admin= '',
    clubInfo.level= -1,
    clubInfo.introduction= '',
    clubInfo.reason='';
    return;
  }
  else{
    showMessageTip('请求失败','error');
    return;
  }
}
else{
  switch (result[0]){
    case 1:
    showMessageTip("服务器响应错误，代码:"+result[1].status+"，请再次尝试",'error');
    break;

    case 2:
    showMessageTip('连接服务器失败,请检查网络','error');
    break;

    case 3:
    showMessageTip('请求发送失败','error');
    break;
  }
}

}


const onCreate = async () => {
  if(clubInfo.name===''||clubInfo.level===-1||clubInfo.admin===''){
    showMessageTip('关键信息不能为空','warning');
    return;
  }
  const applicationInfo= reactive({
    clubName:clubInfo.name,
    introduction:clubInfo.introduction,
    clubLevel:clubInfo.level,
    personId:adminSelect.value
}) 
const result =await api.createClub(applicationInfo);
if(result[0]==0){
  const data =result[1].data;
  if(data.code===1){
    showMessageTip('创建成功','success');
    clubInfo.name='';
    clubInfo.admin= '';
    clubInfo.level= -1;
    clubInfo.introduction= '';
    clubInfo.reason='';
    adminName='';
    return;
  }
  else{
    showMessageTip('请求失败','error');
    return;
  }
}
else{
  switch (result[0]){
    case 1:
    showMessageTip("服务器响应错误，代码:"+result[1].status+"，请再次尝试",'error');
    break;

    case 2:
    showMessageTip('连接服务器失败,请检查网络','error');
    break;

    case 3:
    showMessageTip('请求发送失败','error');
    break;
  }
}
};

const lookupAdmin = async () => {
  const searchResult = await api.getSbIdName({ userName: adminName.value });
  if (searchResult[0] == 0) {
    const data = searchResult[1].data;
    if (data.code == 1) {
      drawer.value = true;
      const researchList = data.data;
      options.value = researchList;
      showMessageTip('查询成功', 'success');
      return;
    }
    else{
      showMessageTip('数据获取失败', 'error');
    }
  }
  else {
    showMessageTip('网络请求失败', 'error');
    return;
  }
};

const newAdmin = () => {
  clubInfo.admin=adminSelect.value;
  const temp =ref();
  temp.value = options.value.find(item =>item["id"]===adminSelect.value );
  adminName.value=temp.value.userName;
}

onMounted(() => {
  const permission = store.getPermission;
  if (permission == 0 || permission == 1) {
    showReact.value = true;
  }
  else if (permission == 2 || permission == 3) {
    showReact.value = false;
  }
});

</script>

<style scoped>
.nameInput {
  max-width: 30%;
}

.Select {
  max-width: 30%;
}

.intro {
  max-width: 70%;
}
.reason{
  max-width: 70%;
}
.card {
  max-width: 700px;
}

.adminInput {
  max-width: 240px;
}
#create {
  position: relative;
  left: 12%;
}

#submit {
  position: relative;
  left: 12%;
}
</style>